#include "DBDataContainer.h"
#include "DataBase.h"

// ----------------------------------- DataContainer ----------------------------------------

namespace db
{
    
ERecordType DataContainer::GetType() const 
{ 
    return RTP_DATA; 
}

void DataContainer::Clear()
{
    if (m_size)
    {
        free(m_data);
        m_size = 0;
    }
}

void DataContainer::Load(ReadStream& file)
{
    Clear();
    file.read(&m_size, sizeof(m_size));
    m_data = malloc(m_size);
    file.read(m_data, m_size);
}

void DataContainer::Save(WriteStream& file) const
{
    file.write(&m_size, sizeof(m_size));
    file.write(m_data, m_size);
}

DataContainer::~DataContainer()
{
    Clear();
}

DataContainer::DataContainer() :
    m_data(NULL),
    m_size(0)
{
    //
}

DataContainer::DataContainer(const void* data, const u32 size) :
    m_data(NULL),
    m_size(size)
{
    m_data = malloc(m_size);
    memcpy(m_data, data, m_size);
}

std::wstring DataContainer::GetAsString() const
{
    return std::wstring((wchar_t*)m_data);
}

bool DataContainer::GetAsBool() const
{
    return m_size > 0;
}
    
u32 DataContainer::GetAsInt() const
{
    if (m_size > sizeof(int))
    {
        return *((int*)m_data);
    }
    return 0;
}
    
f32 DataContainer::GetAsFloat() const
{
    if (m_size > sizeof(float))
    {
        return *((float*)m_data);
    }
    return 0;
}

f64 DataContainer::GetAsDouble() const
{
    if (m_size > sizeof(double))
    {
        return *((double*)m_data);
    }
    return 0;
}
	
u32 DataContainer::GetDataSize() const
{
	return m_size;
}

const void* DataContainer::GetAsData() const
{
	return m_data;
}
    
void DataContainer::Set(const void* data, const u32 size)
{
    Clear();
    m_size = size;
    m_data = malloc(m_size);
    memcpy(m_data, data, m_size);
}

};